/*-----------------------------------------------------------------------------
File: ProjectPath.cpp

History:
02/21/11  jnr	Created
02/23/11  jnr   changed signature of getPath() call. Added error checking.
02/23/11  jnr   modification to error checking
03/01/11  jnr	changed return type of getPath() from void to bool,
				added additional error checking
-----------------------------------------------------------------------------*/

#include "stdafx.h"
#include "ProjectPath.h"

bool getPath(char * &buffer, char *pathFromAddin)
{
	//FIRST: make sure the buffer is NULL to ensure the caller gets a NULL
	//pointer returned upon an error condition.
	buffer = NULL;

	if(!pathFromAddin)
		return false;
	
	//at this point, we need to srip off the filename and extension, which will leave just 
	//the part we want which is the fully qualified path to the active project.
	char *fileNameAndExtension = strrchr(pathFromAddin, '\\');
	if(!fileNameAndExtension)
		return false;
	int lengthToCutOff = strlen(fileNameAndExtension);
	int totalLength = strlen((const char*)pathFromAddin);
	int newLength = totalLength - lengthToCutOff;
	if(newLength <= 0)
		return false;
    buffer = new char[newLength];
	if(!buffer)
		return false;
	strncpy(buffer, (const char*)pathFromAddin, newLength); //copy n bytes to buffer	
	buffer[newLength] = NULL; //add the NULL character	

	return true;
}